<?php

	namespace Home\Controller;
	use Common\Controller\HomecommController;
	header("Content-type: text/html; charset=utf-8"); 
	/**
	 * ThinkPHP Hprose控制器类
	 */
	class IndexController extends HomecommController {

		function _initialize() {
			parent::_initialize();
		}
		function  changecity()
		{
			$ci = I("get.ci");
			$citype = I("get.citype");
			session('mycity',$ci);
			session('citype',$citype);
			@header("location:/index.php?m=Home&c=Index&a=get_car_list&pl=$ci&ci=$citype");
		}
		public function index()
		{
			// echo MODULE_NAME.CONTROLLER_NAME.ACTION_NAME;exit;
			//轮播图
			$bannerModel = M('Banner');
			$banner = $bannerModel->where(array('type'=>1,'is_show'=>1))->order('sort DESC')->limit('3')->select();
			$this->assign ( "banner", $banner );
			
			$count = M ( "Phone" )->where(array('Issale'=>1))->count();
			$this->assign ( "count", $count );

			//品牌
			$carModel = M('Cars');
			$pinpai = $carModel->field("CarBrand,CarK")->group('CarBrand')->select();
			$zm = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
			$arr = array();
			foreach ($pinpai as $key => $val) {
				if(in_array($val['cark'], $zm)){
						// $arr[$val['cark']]['name'] = $val['cark'];
						$arr[$val['cark']][] = $val['carbrand'];
				}else{
					$arr['其他'][] = $val['carbrand'];
				}
			}
			ksort($arr);
			$this->assign('pinpai',$arr);
			$this->display ();
		}

		/**
		*列表页
		**/
		public function get_car_list(){
			
			$lastindex = $_GET['pa'];
			if(empty($lastindex))
			{
				$lastindex = 0;
			}
			else
			{
				$total = $_GET['total'];		
			}
			$maxnum = $_GET['ma'];
			if(empty($maxnum))
				$maxnum = 40;
			/*城市/市场过滤条件*/
			$citys = $_GET['pl'];
			$ordertype = $_GET['or'];
			/*搜索品牌条件*/
			$keybr = $_GET['br'];
			/*搜索车系条件*/
			$keyse = $_GET['se'];
			/*搜索报价条件*/
			$keypr = $_GET['pr'];
			/*搜索箱型条件*/
			$keymo = $_GET['mo'];
			/*搜索标签条件*/
			$keyta = $_GET['ta'];
			/*搜索车龄条件*/
			$keyag = $_GET['ag'];
			/*搜索里程条件*/
			$keylo = $_GET['lo'];
			/*搜索排量条件*/
			$keydi = $_GET['di'];
			/*排放标准过滤条件*/
			$keyno = $_GET['no'];
			/*颜色过滤条件*/
			$keyco = $_GET['co'];
			/*变速箱过滤条件*/
			$keyen = $_GET['en'];
			/*座位过滤条件*/
			$keysea = $_GET['sea'];
			/*燃油过滤条件*/
			$keyoi = $_GET['oi'];
			/*国别过滤条件*/
			$keycon = $_GET['con'];
			/*是否急售*/
			$keywat = $_GET['wat'];
			/*搜索关键词条件*/
			$keyke = $_GET['ke'];
			/*选择的城市/市场*/
			$cityormakert = $_GET['ci'];
			$keybid = $_GET['bid'];
			
			$carModel = D('Common/Cars');
			$result = $carModel->car_list($lastindex,$maxnum,$citys,$ordertype,$keybr,$keyse,$keypr,$keymo,$keyta,$keyag,$keylo,$keydi,$keyno,$keyco,$keyen,$keysea,$keyoi,$keycon,$keywat,$keyke,$cityormakert);
			if($result["Ret"] != "0")
			{
				echo $result["Msg"];
				return;
			}
			if($lastindex == 0)
			{
				$total = $result["Msg"];
			}
			cookie('or',$ordertype);cookie('br',$keybr);cookie('se',$keyse);
			cookie('pr',$keypr);cookie('mo',$keymo);cookie('ta',$keyta);
			cookie('ag',$keyag);cookie('lo',$keylo);cookie('di',$keydi);
			cookie('no',$keyno);cookie('co',$keyco);cookie('en',$keyen);
			cookie('sea',$keysea);cookie('oi',$keyoi);cookie('con',$keycon);
			cookie('wat',$keywat);cookie('ke',$keyke);cookie('bid',$keybid);
			cookie('total',$total);
			$showreset = "1";
			$showke = $keyke;
			if(mb_strlen($showke, 'utf8') > 5) 
			$showke = mb_substr($showke, 0, 5, 'utf8').'...';
			session('mycity',$citys);
			session('citype',$cityormakert);
			$keysearchlog = cookie('keysearchlog');
			$this->assign ( "keysearchlog", $keysearchlog );
			$this->assign("maxcount",$result["Msg"]);
			$datalist = $result["Data"];
			// echo "<pre>";print_r($datalist);exit;
			$this->assign ( "datalist", $datalist );
			$datacount = count($datalist);
			$this->assign ( "datacount", $datacount );
			$showpr = "";
			$showprmin = "";
			$showprmax = "";
			if(!empty($keypr))
			{
				$showpr = str_replace("<","小于",$keypr);
				$showpr = str_replace(">","大于",$showpr);
				$showpr .= '万';
				
				if(false !== stripos($keypr,"<")) { 
					$showprmin = "";
					$showprmax = substr($keypr,1);
				}
				if(false !== stripos($keypr,">")) { 
					$showprmax = "";
					$showprmin = substr($keypr,1);
				}
				if(false !== stripos($keypr,"-")) { 
					$pos =  stripos($keypr,"-");
					$showprmin = substr($keypr,0,$pos);
					$showprmax = substr($keypr,$pos + 1);
				}
			}
			$this->assign ( "showprmin", $showprmin );
			$this->assign ( "showprmax", $showprmax );
			$this->assign ( "showpr", $showpr );
			
			$showage = "";
			if(!empty($keyag ))
			{
				$showage = str_replace("<","小于",$keyag);
				$showage = str_replace(">","大于",$showage);
				$showage .= '年';
			}
			$this->assign ( "showage", $showage );
			
			$showlong = "";
			if(!empty($keylo ))
			{
				$showlong = str_replace("<","小于",$keylo);
				$showlong = str_replace(">","大于",$showlong);
				$showlong .= '万公里';
			}
			$this->assign ( "showlong", $showlong );
			$this->assign ( "showke", $showke );
			
			
			$showdi = "";
			if(!empty($keydi ))
			{
				$showdi = str_replace("<","小于",$keydi);
				$showdi = str_replace(">","大于",$showdi);
				$showdi .= 'L';
			}
			
			
			$this->assign ( "showdi", $showdi );
			
			if($keysea =='2-2')
			{
				$selectsea='2座';
			}
			elseif($keysea =='4-4')
			{
				$selectsea='4座';
			}
			elseif($keysea =='5-5')
			{
				$selectsea='5座';
			}
			elseif($keysea =='6-6')
			{
				$selectsea='6座';
			}
			elseif($keysea=='>7')
			{
				$selectsea='7座以上';
			}
			else
			{
				$titlesea =$keysea.'座';
			}
			$this->assign ( "titlesea", $selectsea );
			$rowcount =ceil(cookie('total')/40);
			$this->assign ( "rowcount", $rowcount );
			if(  empty($keybr) && empty($keyse) &&
				empty($showpr) && empty($keymo) &&
				empty($keyta) && empty($keyen) &&
				empty($keyag) && empty($keylo) &&
				empty($keyoi) && empty($keycon) &&
				empty($keywat) && empty($keysea) && empty($showke) )
			{
				$showreset = "0";
			}
			$this->assign ( "showreset", $showreset );
			$emptylist = "<a href='tel:400-100-1313'>更多最新车源信息联系400-100-1313</a>";
			$this->assign ( "emptylist", $emptylist );
			
			$this->assign("getData",$_GET);
			if($_GET['show'] == 'index'){
				$this->display ();//首页列表
			}else{
				//品牌
				$carModel = M('Cars');
				$pinpai = $carModel->field("CarBrand,CarK,count(1) as num")->group('CarBrand')->order('num DESC')->select();
				$zm = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
				$pp_zm = array();
				foreach ($pinpai as $key => $val) {
					if(in_array($val['cark'], $zm)){
							$pp_zm[$val['cark']][] = $val['carbrand'];
					}else{
						$pp_zm['其他'][] = $val['carbrand'];
					}
				}
				ksort($pp_zm);
				$this->assign('pp_zm',$pp_zm);
				$this->assign('pp_arr',$pinpai);

				//车系
				if($keybr){
					$seriesWhere['CarBrand'] = $keybr;
				}else{
					$seriesWhere= '1=1';
				}
				$series =$carModel->distinct(true)->field('CarSeries')->where($seriesWhere)->select();
				// echo "<pre>";print_r($series);exit;
				$this->assign('series',$series);
				$this->display ('getlist');//列表页
			}
		}

		public function about(){
			$this->display();
		}

		/**
		*详情页
		**/
		public function builddetail(){
			$id=I("get.id");
			$car = M ( "Cars" );
			$result =$car->where(array('Id'=>$id))->find();
			$this->assign ( "id", $id );
			$this->assign ( "result", $result );
			$licdate = date('Y年m月d日',strtotime($result['carlicdate']));
			$this->assign ( "licdate", $licdate );
			$carconfig = M ( "Carconfig" );
			$cresult =$carconfig->where(array('Id'=>$result['cfid']))->find();
			//var_dump($cresult);exit;
			$this->assign ( "cresult", $cresult );
			
			$images =M ( "Carimage" );
			$imgresult =$images->where(array('CarId'=>$id))->order('ImageType asc')->select();
			$this->assign ( "imgresult", $imgresult );
			$total = count($imgresult);
			$this->assign ( "total", $total );
			
			//print_r(count($imgresult));exit;
			$link = M ( "Link" );
			$linklist =$link->order('sort desc')->select();
			$this->assign ( "linklist", $linklist );
			$this->display ();
		}

		public function seller(){
			$this->display();
		}
	}
?>